﻿using System;
using System.Data;
using System.Text;

namespace YG.CommonFuncs
{
    /// <summary>
    /// data转换成html的table
    /// </summary>
    public static class Data2HTMLTable
    {
        public static string DataTableToHTMLTable(DataTable dt)
        {
            try
            {
                StringBuilder sB = new StringBuilder();

                sB.AppendFormat($"<table align=\"center\" border=\"1\" >  ");
                sB.AppendFormat($"<caption>{dt.TableName}</caption> ");
                sB.AppendFormat($"<tr bgcolor =\"#3498db\">");
                sB.AppendFormat($" ");
                foreach (DataColumn column in dt.Columns)
                {
                    sB.AppendFormat($"<th>{column.ColumnName}</th> ");
                }
                sB.AppendFormat($" </tr>");

                foreach (DataRow row in dt.Rows)
                {
                    sB.AppendFormat($" <tr bgcolor =\"#bdc3c7\">");
                    foreach (DataColumn column in dt.Columns)
                    {
                        string columnvalue = row[column] != null ? row[column].ToString() : "";

                        sB.AppendFormat($"<td>{columnvalue } </td>");
                    }
                    sB.AppendFormat($" </tr>");
                }

                sB.AppendFormat($"</table> ");

                return sB.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string DataSetToHTMLTable(DataSet dS)
        {
            try
            {
                StringBuilder sB = new StringBuilder();

                foreach (DataTable dt in dS.Tables)
                {
                    sB.AppendFormat($"<table width = \"80%\" height = \"50%\" align=\"center\" border=\"1\" > ");
                    sB.AppendFormat($"<caption>{dt.TableName}</caption> ");
                    sB.AppendFormat($" <thead>");
                    sB.AppendFormat($" <tr bgcolor =\"#3498db\">");
                    sB.AppendFormat($" ");
                    foreach (DataColumn column in dt.Columns)
                    {
                        sB.AppendFormat($"<th>{column.ColumnName}</th> ");
                    }
                    sB.AppendFormat($" </tr>");
                    sB.AppendFormat($" </thead>");

                    foreach (DataRow row in dt.Rows)
                    {
                        sB.AppendFormat($" <tr bgcolor =\"#bdc3c7\">");
                        foreach (DataColumn column in dt.Columns)
                        {
                            string columnvalue = row[column] != null ? row[column].ToString() : "";

                            sB.AppendFormat($"<td>{columnvalue } </td>");
                        }
                        sB.AppendFormat($" </tr>");
                    }
                    sB.AppendFormat($"</table> ");
                    sB.AppendFormat($"<br/> ");
                }
                return sB.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}